<template>
    <div>
        <book-read :title="title" :currentChapterIndex="currentChapterIndex" :chapters="chapters" :curBook="curBook" :read-content="readContent" :bookId="bookId" @show-menu="showMenu"
                   @nextChapter="nextChapter"
                   @preChapter="preChapter"
                   @goRead="goRead" @onSort="onSort" @updateBook="updateBook">
        </book-read>
        <wxc-loading :show="isShow" ></wxc-loading>
    </div>
</template>

<script>
    import BookRead from "../components/book/BookRead"
    import {book_chapter, book_content_by_chapter_link} from '../store/actions'
    import {SHELF_BOOK,HISTORY_BOOK,CUR_BOOK} from '../store/mutation-types';
    import {SET_BOOK,SET_HISTORY_BOOK} from '../store/mutations';
    import {getCache,setCache} from '../util/storageUtils';
    import {WxcLoading} from 'weex-ui'
    const modal = weex.requireModule('modal');

    export default {
        name: "read",
        components: {
            BookRead,WxcLoading
        },
        data() {
            return {
                readContent: undefined,
                bookId: '',
                sourceId: '',
                content: undefined,
                sort: false,
                chapters: undefined,
                title: undefined,
                currentLink: undefined,
                currentChapterIndex: 0,
                selfBook: {},
                historyBook: {},
                curBook: {
                    readChapter: 0,
                    sort: false,
                    isInShelf: false,
                },
                sortNo: {
                    true: -1,
                    false: 1,
                },
                isShow:false,
            }
        },
        mounted() {
            this.bookId = this.$route.query.bookId || '';
            this.sourceId = this.$route.query.sourceId || '';
            this.currentChapterIndex = parseInt(this.$route.query.chapterIndex) || 0;
            getCache(HISTORY_BOOK).then( value => {
                this.historyBook = JSON.parse(value) || {};
                let temp = this.historyBook[this.bookId];
                if (temp) {
                    this.curBook=temp;
                    this.curBook.isInShelf = false;
                    this.curBook.readChapter = temp.readChapter;
                    this.curBook.sort = temp.sort;
                    this.historyBook[this.bookId] = this.curBook;
                    if(this.currentChapterIndex === 0 && this.curBook.readChapter){
                        this.currentChapterIndex = this.curBook.readChapter;
                    }
                }
            });
            getCache(SHELF_BOOK).then(value =>{
                this.selfBook = JSON.parse(value) || {};
                let temp = this.selfBook[this.bookId];
                if (temp) {
                    this.curBook.isInShelf = true;
                    this.selfBook[this.bookId] = this.curBook;
                }
            });
            this.getChapter();
        },
        methods: {
            storeBook(){
                setCache(SHELF_BOOK,this.selfBook);
                setCache(HISTORY_BOOK,this.historyBook);
                setCache(CUR_BOOK,this.curBook);
            },
            read() {
                let index = this.currentChapterIndex;
                if (index < 0) {
                    modal.toast({message: "没有更多了"});
                    this.currentChapterIndex = 0;
                } else if (index >= this.chapters.length) {
                    modal.toast({message: "没有更多了"});
                    this.currentChapterIndex = this.chapters.length-1;
                } else {
                    this.isShow = true;
                    let readChapter = this.sort ?(this.chapters.length - index - 1):index;
                    this.curBook.readChapter = readChapter;
                    this.storeBook();
                    let chapterObj =  this.chapters[index];
                    book_content_by_chapter_link({
                        link: chapterObj.link,
                        callback: (data) => {
                            this.readContent = data;
                            this.readContent.title = chapterObj.title;
                        },
                        after: ()=>{
                            this.isShow = false;
                        }
                    });
                }
            },
            getChapter() {
                this.isShow = true;
                book_chapter({
                    bookId: this.bookId,
                    sourceId: this.sourceId,
                    callback: (data) => {
                        this.content = data;
                        this.bookId = this.content.book;
                        this.sourceId = this.content._id;
                        this.title = this.content.bookName;
                        this.chapters = this.content.chapters;
                        this.curBook.sourceId = this.sourceId;
                        this.historyBook[this.bookId] = this.curBook;
                        if(this.selfBook[this.bookId]){
                            this.selfBook[this.bookId] = this.curBook;
                        }
                        this.storeBook();
                        this.read();
                    },
                    after: ()=>{
                        this.isShow = false;
                    }
                });
            },
            showMenu() {

            },
            nextChapter() {
                this.currentChapterIndex+=this.sortNo[this.sort];
                this.read();
            },
            preChapter() {
                this.currentChapterIndex-=this.sortNo[this.sort];
                this.read();
            },
            updateBook() {
                this.selfBook[this.bookId] = this.curBook;
                this.historyBook[this.bookId] = this.curBook;
                SET_BOOK(this.selfBook);
                SET_HISTORY_BOOK(this.historyBook)
            },
            goRead(obj){
                this.currentChapterIndex = obj.chapterIndex;
                this.read();
            },
            onSort(){
                this.sort = !this.sort;
                this.currentChapterIndex = this.chapters.length - this.currentChapterIndex - 1;
            }
        }
    }
</script>

<style scoped>

</style>